package cn.com.nes.site.controller.crm.customer;

import cn.com.nes.common.web.CommonResult;
import cn.com.nes.site.controller.crm.customer.vo.GetCustomerDetailVo;
import cn.com.nes.site.entity.PageData;
import cn.com.nes.site.entity.vo.NullVO;
import cn.com.nes.site.entity.vo.crm.*;
import cn.com.nes.site.service.crm.customer.CrmCustomerService;
import cn.com.nes.site.service.crm.customer.bo.GetCustomerDetailBO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Api(tags = {"销售管理-客户管理"})
@Controller
@RequestMapping("/v2/crm/customer")
public class CustomerController {

    @Resource
    private CrmCustomerService crmCustomerService;

    @ApiOperation("认领客户")
    @RequestMapping(value = "claimCustomer", method = RequestMethod.POST)
    public @ResponseBody PageData claimCustomer(@RequestBody @Validated ClaimCustomerVo vo) throws Exception {
        return crmCustomerService.claimCustomer(vo);
    }

    @ApiOperation("批量分配（转移）客户")
    @RequestMapping(value = "assignCustomers", method = RequestMethod.POST)
    public @ResponseBody PageData assignCustomers(@RequestBody @Validated AssignCustomersVo vo) throws Exception {
        return crmCustomerService.assignCustomers(vo);
    }

    @ApiOperation("线索转客户")
    @RequestMapping(value = "clueToCustomer", method = RequestMethod.POST)
    public @ResponseBody PageData clueToCustomer(@RequestBody @Validated ClueToCustomerVo vo) throws Exception {
        return crmCustomerService.clueToCustomer(vo);
    }

    @ApiOperation(value = "客户保存")
    @RequestMapping(value = "saveCustomer", method = RequestMethod.POST)
    public @ResponseBody CommonResult<?> saveCustomer(@RequestBody @Validated SaveCustomerVo vo) throws Exception {
        return CommonResult.data(crmCustomerService.customerSave(vo.convertToBo()).getUuid());
    }

    @ApiOperation("全部客户分页列表")
    @RequestMapping(value = "allCustomerPage", method = RequestMethod.GET)
    public @ResponseBody CommonResult<?> allCustomerPage(QueryCustomerPageVo vo) throws Exception {
        return CommonResult.page(crmCustomerService.customerPage(vo.convertToBo()));
    }

    @ApiOperation("我的客户分页查询")
    @RequestMapping(value = "myCustomerPage", method = RequestMethod.GET)
    public @ResponseBody CommonResult<?> myCustomerPage(QueryCustomerPageVo vo) throws Exception {
        // 当前登录用户设置为客户负责人
        vo.setCustomerOwnerId(vo.getOperateUserId());
        return CommonResult.page(crmCustomerService.customerPage(vo.convertToBo()));
    }

    @ApiOperation(value = "查询客户详情", notes = "销售管理->客户管理->客户详情")
    @RequestMapping(value = "getCustomerDetail", method = RequestMethod.GET)
    public @ResponseBody CommonResult<?> getCustomerDetail(@Validated GetCustomerDetailVo vo) throws Exception {
       return CommonResult.data(crmCustomerService.getCustomerDetail(vo.convertToBo(new GetCustomerDetailBO())));
    }

    @ApiOperation("补充客户信息")
    @RequestMapping(value = "/replenishCustomerInfo", method = RequestMethod.POST)
    public @ResponseBody PageData replenishCustomerInfo(@RequestBody @Validated ReplenishCustomerInfoVO vo) throws Exception {
        return crmCustomerService.replenishCustomerInfo(vo);
    }

    @ApiOperation("查询补充客户信息相关下拉框")
    @RequestMapping(value = "/replenishCustomerDropdownBox", method = RequestMethod.GET)
    public @ResponseBody PageData replenishCustomerDropdownBox(NullVO vo) throws Exception {
        return crmCustomerService.replenishCustomerDropdownBox(vo);
    }

}
